<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>客户端</title>
</head>

<body>
    <button>发送数据到服务器</button>
    <script src="https://cdn.bootcdn.net/ajax/libs/socket.io/4.1.3/socket.io.js"></script>
    <script>
        const socket = io();

        // 发送消息到客户端
        document.querySelector('button').onclick = function () {
            socket.emit("test", '客户端发送的消息')
        }

        // 接收服务端消息
        socket.on('test', (data) => {
            console.log('来自服务端的消息：', data);
        })

    </script>
</body>

</html>